package com.john.booter.domain.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 系统角色
 * <pre>权限模型：用户-组-角色</pre>
 * @author weny
 * @datetime 2015年9月18日 下午2:56:39
 */
@Entity
@Table(name = "t_role")
public class Role implements Serializable {

	private static final long serialVersionUID = -5746095157106377461L;
	
	@Id
	private String code;

	@Column(nullable = true)
	private String description;
	
	@NotEmpty
	@Column(nullable = true)
	private String name;

	@Column(updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date createTime;
	
	@Column(updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date updateTime;
	
	@ManyToMany
	@JoinTable(
	        name="t_role_option",
	        joinColumns=@JoinColumn(name="role_code", referencedColumnName="code"),
	        inverseJoinColumns=@JoinColumn(name="option_code", referencedColumnName="code")
	    )
	private Set<Option> options;
	
	@Column
	private String state;


	@PreUpdate
	public void preUpdate() {
		updateTime = new Date();
	}

	@PrePersist
	public void prePersist() {
		Date now = new Date();
		createTime = now;
		updateTime = now;
	}


	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public Set<Option> getOptions() {
		return options;
	}

	public void setOptions(Set<Option> options) {
		this.options = options;
	}
	
}
